<?php
/**
 * 摇一摇
 * 
 * @filepath Apps/Wechat/Controller/ShakeController.class.php
 * @author Zix <zix2002@gmail.com>
 * @version  1.0 , 2015-09-13
 */
namespace Wechat\Controller;

class ShakeController extends WechatController {

  /**
   * 活动首页
   */
  public function index() {
    
    //检查参数是否接收成功
    if ( empty( $this->mer_id ) || empty( $this->wx_id ) ) {
      die( 'param error' ); //未取到微信信息
    }    
    
    $event_id = I('get.event_id' , '');
    if ( empty( $event_id ) ) {
      die('unkown event');
    }
   
    //取用户信息
    $userinfo = $this->_get_user_info();
    $url_query = getenv('QUERY_STRING');
    $data['user_info'] = $userinfo;
    
    //取活动信息
    $WxEventShake = \Common\Service\WxEventShakeService::instance();
    $data['event_data'] = $WxEventShake->get_by_id($event_id);
    
    //取奖品信息
    $WxEventShakeAward = \Common\Service\WxEventShakeAwardService::instance();
    $data['award_data'] = $WxEventShakeAward->get_by_cond(array(
      'event_id' => $event_id ,
      'status'   => 1 
    ));
    
    $data['config'] = array(
      'do_lottery_url' => U('Wechat/Shake/do_lottery') . "/$event_id/?" . $url_query ,
      'my_award_url' => U('Wechat/Shake/my_award/') .'/'. $data['user_info']['user_id'] . $url_query 
    );
    
    $this->assign($data);
    $this->display();
  }
  
  function do_lottery( $event_id ) {
    
    $userinfo = $this->_get_user_info();

    if( empty($userinfo) ) {
      //$this->ajaxReturn(ajax_arr('请先关注'));
      $userinfo['user_id'] = 1 ;
    }
    
    $WxEventShake = \Common\Service\WxEventShakeService::instance();
    $ret = $WxEventShake->do_lottery( $event_id , $userinfo['user_id'] );
    
    $this->ajaxReturn( $ret );
  }
  
  function my_award( $user_id ) {
    $WxEventShakeUser = \Common\Service\WxEventShakeUserService::instance();
    $config = array(
      'event_id' => I('get.event_id' , 0 ),
      'user_id'  => $user_id ,
      'without_no_award' => TRUE ,
    );
    
    $data['rows'] = $WxEventShakeUser->get_by_cond( $config ) ;
    $config['count'] = TRUE ;
    $data['total'] = $WxEventShakeUser->get_by_cond( $config ) ;
    
    $this->assign($data);
    $this->display();
  }
  


  
}
